
#!/bin/bash

## author CC
## notice: you must been have mysql-5.5.50-linux2.6-x86_64.tar.gz in the /usr/local/src dir
set -e

mysqlfile='mysql-5.5.50-linux2.6-x86_64'
dir=/usr/local/src
cd $dir
tar zxvf `echo $mysqlfile`.tar.gz
mv $mysqlfile /usr/local/mysql
mkdir -p /data/mysql/data
mkdir -p /data/mysql/log-bin
mkdir -p /data/mysql/relay_log/relay-bin
## 创建用户
useradd -s /sbin/nologin mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
## 拷贝配置文件
cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
## 修改启动文件
if [ -f /etc/init.d/mysqld ]; then
    sed -i '40,50s#basedir=#basedir=/usr/local/mysql#g' /etc/init.d/mysqld
    sed -i '40,50s#datadir=#datadir=/data/mysql/data#g' /etc/init.d/mysqld
fi
## 初始化数据库
cd /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data
## 加入开机启动
echo "------------step 1:basedir and datadir have changed-----------"
chkconfig --add mysqld
chkconfig mysqld on
echo "------------step 2: mysql5.5.50 has been installed"
